package com.example.play.adapter

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import androidx.recyclerview.widget.RecyclerView
import com.bawei.common.entity.SimpleVideoEntity
import com.example.image.ImgOptions
import com.example.image.LoadImageUtil
import com.example.play.databinding.ItemPlayVideoBinding

class PlayVideoAdapter constructor(private val list:MutableList<SimpleVideoEntity>):RecyclerView.Adapter<PlayVideoAdapter.VideoPlayViewHodel>(){

    private var itemClickListener :OnItemChildListener ?= null

    fun setOnItemClickListener(listener: OnItemChildListener){
        itemClickListener = listener
    }

    inner class VideoPlayViewHodel(val dataBinding:ItemPlayVideoBinding):RecyclerView.ViewHolder(dataBinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoPlayViewHodel {
        return VideoPlayViewHodel(ItemPlayVideoBinding.inflate(LayoutInflater.from(parent.context),parent,false))
    }

    override fun getItemCount(): Int = list.size
    override fun getItemViewType(position: Int): Int {
        return 0
    }
    override fun onBindViewHolder(holder: VideoPlayViewHodel, position: Int) {
        val entity = list[position]

        //视频播放
        holder.dataBinding.gsyPlayer.apply {
            setUp(entity.videopath,true,"")
            backButton.visibility = View.GONE
            fullscreenButton.visibility = View.GONE
        }
//设置作者头像
        LoadImageUtil.loadUrl(
            holder.dataBinding.root.context,
            entity.avatar_url,
            holder.dataBinding.ivHead,
            ImgOptions.Builder.isCircle(true).build()
        )
        var playImageView = holder.dataBinding.ivMusicPlayer
        //播放碟片图片
        LoadImageUtil.loadUrl(
            holder.dataBinding.root.context,
            entity.avatar_url,
            playImageView,
            ImgOptions.Builder.isCircle(true).build()
        )
        //旋转动画
        ObjectAnimator.ofFloat(playImageView,"Rotation",0F,360F).apply {
            duration = 1500 //执行的事件
            repeatCount = ValueAnimator.INFINITE //无线循环
            interpolator = LinearInterpolator()//匀速效果
        }.start()


        //设置子组件的点击事件
        holder.dataBinding.apply {
            //点击封面头像
            ivHead.setOnClickListener{
                itemClickListener?.onItemChildListener(it,position)
            }
            //点击关注
            ivAdd.setOnClickListener{
                itemClickListener?.onItemChildListener(it,position)
            }
            //点击点赞
            ivDianZanBtn.setOnClickListener{
                itemClickListener?.onItemChildListener(it,position)
            }
            //点击评论
            ivPingLunBtn.setOnClickListener{
                itemClickListener?.onItemChildListener(it,position)
            }
            //点击分享按钮
            ivShareBtn.setOnClickListener{
                itemClickListener?.onItemChildListener(it,position)
            }
            //点击收藏按钮
            ivShouCangBtn.setOnClickListener{
                itemClickListener?.onItemChildListener(it,position)
            }
            //点击发送按钮
            ivSend.setOnClickListener{
                itemClickListener?.onItemChildListener(it,position)
            }
        }



    }

    interface OnItemChildListener{
        fun onItemChildListener(v:View,positions:Int)
    }

}